TrafficWatch server mode
========================

Run TrafficWatch as a service and open its console from any browser on your network.

The desktop app needs a graphical session. Server mode does not: it answers and
records DNS queries exactly the same way, keeps running when nobody is signed in
to the server, and shows the same screen in your browser instead of a window —
over HTTPS, behind a username and password.

Everything below quotes the install path: it contains a space.

What a minimal server still needs
---------------------------------
Server mode opens no window and contacts no X server, but the program is one
binary and the system loads every library that binary is linked against before
it starts. So GTK, X11, NSS and ALSA have to be installed even though nothing
draws. The .deb package lists them as dependencies and apt installs them for
you. If they are missing anyway, trafficwatch-headless says so and prints the
apt / dnf line rather than leaving you with a bare "error while loading shared
libraries".

Licence agreement
-----------------
TrafficWatch does not answer DNS until the licence agreement has been
accepted. The agreement is EULA.txt in this folder (also
https://meshwatch.app/terms). Read it, then either

  - start with --accept-eula, which records your acceptance in the data folder
    (eula.json) and prints one line saying so, or
  - start without it and sign in to the console as an administrator: it shows
    the agreement first, and DNS answering starts when you select "I agree".

The service unit (trafficwatch-headless.service) passes --accept-eula:
installing and enabling it means you accept the agreement. When a later
version changes the agreement, it asks again.

Try it in the foreground first
------------------------------
Port 53 is privileged, so a plain user cannot bind it. For a first look, use a
high port and the loopback address (no root needed):

    "/opt/Network TrafficWatch/trafficwatch-headless" --accept-eula --dns-port 5353 --dns-host 127.0.0.1

It prints the console address and the certificate fingerprint:

    console  https://dns01:8053/
             https://192.168.1.10:8053/
    certificate  self-signed (~/.trafficwatch/tls/cert.pem)
             SHA-256 3A:1F:...:C4
    accounts NONE YET — create the first administrator: ...

Open https://localhost:8053/ in a browser on the same machine and create the
first administrator there. Send a query to see it appear:

    dig @127.0.0.1 -p 5353 example.com

The screen's "Send a test query" button does the same thing from the browser.

Run it as a service (port 53)
-----------------------------
See trafficwatch-headless.service in this folder — the commands are at the top
of that file. Two things are specific to this product:

  * The unit keeps  AmbientCapabilities=CAP_NET_BIND_SERVICE  uncommented.
    That is what lets the service user open port 53. Do not remove it.

  * systemd-resolved on Ubuntu already holds 127.0.0.53:53, so binding every
    address fails with EADDRINUSE. The unit's ExecStart therefore passes
    --dns-host <LAN address>: edit that one address to this server's own
    (`ip -4 addr`), and the two coexist. The alternative — DNSStubListener=no
    in /etc/systemd/resolved.conf — also works but then the server's own
    /etc/resolv.conf needs attention.

Then point your router's DHCP DNS setting at that LAN address. Every device
that renews its lease starts sending its lookups here.

Open TCP 8053 (the console) and port 53 over both UDP and TCP in the server's
firewall if it has one (large DNS answers are retried over TCP):

    sudo ufw allow 8053/tcp && sudo ufw allow 53/udp && sudo ufw allow 53/tcp   # Ubuntu

Create the first administrator
------------------------------
Nobody can sign in until an administrator exists, and for safety the first one
can only be created from the server itself — otherwise anyone on the network
could claim it before you. Either run, on the server:

    sudo -u trafficwatch TRAFFICWATCH_DATA_DIR=/var/lib/trafficwatch/.trafficwatch \
        "/opt/Network TrafficWatch/trafficwatch-headless" --create-admin admin

(it asks for the password twice and does not show it; at least 12 characters),
or open https://localhost:8053/ in a browser on the server, or tunnel to it from
your PC and open the same address there:

    ssh -L 8053:127.0.0.1:8053 you@dns01

Open the console from your PC
-----------------------------
Open https://<server>:8053/ in your browser. The certificate is self-signed, so
the browser warns that it does not know it. Open the certificate details, check
that its SHA-256 fingerprint is the one in the service log (the sign-in page
shows it too), then continue and sign in. A plain http:// address on the same
port is redirected to https://.

Accounts and roles
------------------
  Administrator   everything: labels, alert rules, e-mail, the licence, accounts
  Viewer          sees the query log, devices and alerts, exports the log —
                  cannot change anything (the server refuses it and the screen
                  says so)

Add people in the console under Accounts (top right), or on the server:

    "/opt/Network TrafficWatch/trafficwatch-headless" --add-user alice --role viewer
    "/opt/Network TrafficWatch/trafficwatch-headless" --list-users
    "/opt/Network TrafficWatch/trafficwatch-headless" --reset-password alice     # forgotten password; signs her out everywhere

Run these as the service user with the same data folder (as above). Sessions
end after 12 hours without activity, on Sign out, and when the service restarts.
Ten failed sign-ins a minute from one address, or five in a row for one account,
lock further attempts for a while.

HTTPS
-----
  - The self-signed certificate lives in <data folder>/tls (cert.pem, key.pem,
    mode 0600). It is valid for 825 days and is replaced automatically 30 days
    before it expires — the log then says the fingerprint is new.
  - Your own certificate: --cert /path/fullchain.pem --key /path/privkey.pem
    (the service user must be able to read both).
  - --insecure-http serves plain HTTP. Use it only behind an HTTPS reverse proxy
    on the same machine, together with --ui-host 127.0.0.1. Requests that arrive
    through a proxy (X-Forwarded-For) never count as "this machine", so create
    the first administrator with --create-admin.

Options:                        "/opt/Network TrafficWatch/trafficwatch-headless" --help
Lost the address or fingerprint: "/opt/Network TrafficWatch/trafficwatch-headless" --show-url

Upgrading from 1.1.8 (token sign-in)
------------------------------------
1.1.8 opened the screen with a one-time token (ui-token in the data folder). That
token no longer opens anything; the console now uses accounts, and the log says
so on the first start. Create the first administrator as above.

  - Your existing unit file in /etc/systemd/system still says --ui-host 127.0.0.1,
    so the console stays on this machine until you remove that option (the new
    unit file in this folder no longer has it).
  - If you put an HTTPS reverse proxy in front as 1.1.8 suggested, either point it
    at https://127.0.0.1:8053 (the console now speaks HTTPS itself), or add
    --insecure-http to keep plain HTTP behind the proxy.

Secrets on a server
-------------------
There is no keyring on a server, so the licence key, SMTP password and Central
token are stored as plain files readable only by the service user (mode 0600)
under the data folder. Account passwords are stored only as scrypt hashes
(users.json, mode 0600).

Notifications
-------------
There is no desktop to notify. A rule that would have raised a desktop
notification writes one line to the journal instead (journalctl -u
trafficwatch-headless). E-mail alerts and MeshWatch Central work as on the
desktop — Central is the intended alert path for a server.

Where the data is — and the licence key
---------------------------------------
The data folder is ~/.trafficwatch of the user that runs it (or
TRAFFICWATCH_DATA_DIR). As a service that is /var/lib/trafficwatch/.trafficwatch.

If you ran the Linux desktop app with sudo (the README's route to port 53), its
data is in /root/.trafficwatch — a different folder from the service's. The
licence key is therefore not shared automatically: paste it again in the console
(plan badge in the title bar). The same key may be active in both places.
